<?php

require_once APPPATH."/controllers/BasePriv.php";
require_once APPPATH."/libraries/phpqrcode/phpqrcode.php";
require_once APPPATH."/libraries/aws/aws-autoloader.php";
require_once APPPATH."/libraries/Tools.php";
use Aws\S3\S3Client;
class Words extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_type_sub_model","table");
        $this->type_sub="type_sub";//话题表
        $this->detail_msg="detail_msg";//操作详细表
    }

    /**
     * 话题主页
     */
    public function index(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;
        //上下线状态筛选
        if(!empty($_GET['online'])){
            $arr['online']=$_GET['online'];
        }
        //分类筛选
        if(!empty($_GET['join_type'])){
            $arr['join_type']=$_GET['join_type'];
        }
        $res=$this->table->all($arr);
        //查询分类
        $type=$this->table->types(3);
//        var_dump($type);exit;
        //查询话题
        $type_sub=$this->table->type_sub("",2);
        //显示话题和分类
        if(!empty($res['row'])){
            foreach($res['row'] as $k=>&$v){
                foreach($type as $kk=>$vv){
                    if($v['sub_type']==$vv['id']){
                        $v['video_join_type_name']=$vv['name'];
                    }
                }
//                $len=strlen($v['sort']);
//                $length=substr($v['sort'],0,1);
//                if($length==0){
//                    $v['sort']=substr($v['sort'],1,$len);
//                }
            }
        }
        $data['data']=$res;
        $data['page']=$arr['page'];
        $data['limit']=$arr['limit'];
        $data['type']=$type;
        $this->layout("type/word/index",$data);
    }

    /**
     * 新增话题
     */
    public function add_word(){
        $this->layout("type/word/add_word");
    }

    /**
     * 保存新增话题
     */
    public function save_word(){
        if(empty($_POST['sub_title'])){
            response(-1,"话题名称不能为空");
        }
        if(mb_strlen($_POST['sub_title'])>5){
            response(-1,"话题名称不能大于5个字");
        }
        if(empty($_POST['sub_pic'])){
            $data['sub_pic']=$_POST['sub_pic'];
        }
        if(!empty($_POST['sub_desc'])){
            if(mb_strlen($_POST['sub_desc'])>50){
                response(-1,"话题描述不能大于50个字");
            }
            $data['sub_desc']=$_POST['sub_desc'];
        }
        //先去查询下是否已经新建了这个话题
        $is_save1=$this->table->one("type_sub",['sub_title'=>$_POST['sub_title']]);
        if($is_save1){
            response(-1,"该话题已经存在");
        }
        $data['sub_title']=$_POST['sub_title'];
        $data['sub_pic']=$_POST['sub_pic'];
        $data['sub_type']=$_POST['sub_type'];
        $data['online']=2;
        $data['sort']=999999999;
        $data['add_times']=date("Y-m-d H:i:s",time());
//        var_dump($data);exit;
        $res=$this->table->add("type_sub",$data);
        if($res){
//            response(0,"添加成功",base_url("word/words/edit_word?id={$res}"));
            response(0,"添加成功",base_url("word/words/index"));
        }else{
            response(-1,"添加失败");
        }
    }

    /**
     * 修改话题
     */
    public function edit_word(){
        $id=$_REQUEST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        //这是话题的基本信息
        $res=$this->table->one("type_sub",['id'=>$id]);
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 5;
        $arr['join_type_sub']=$res['id'];
        if($res['is_other']==999999999){
            $arr['is_other']=2;
        }
        //在根据话题的id去用户投稿列表属于该话题的
        $res1=$this->table->all_type_sub("user_material",$arr);
        //查询分类
        $types=$this->table->types();
        //查询所有话题
        $huati=$this->table->alls($this->type_sub,"","id,sub_title");
        if(!empty($res1['row'])){
            foreach($res1['row'] as $k=>&$v){
                if(!empty($v['old_join_type'])){
                    foreach($types as $kk=>$vv){
                        if($vv['id']==$v['old_join_type']){
                            $v['old_join_type']=$vv['name'];
                        }
                    }
                }
                if(!empty($v['old_join_type_sub'])){
                    foreach($huati['row'] as $kkk=>$vvv){
                        if($vvv['id']==$v['old_join_type_sub']){
                            $v['old_join_type_sub']=$vvv['sub_title'];
                        }
                    }
                }
            }
        }
        $data['data']=$res;
        $data['data1']=$res1;
        $data['type']=$types;
        $data['page']=$arr['page'];
        $data['limit']=$arr['limit'];
        $data['type_sub_id']=$id;
        $this->layout("type/word/edit_word",$data);
    }

    /**
     * 保存话题修改
     */
    public function save_edit_word(){
        if(empty($_POST['sub_title'])){
            response(-1,"话题名称不能为空");
        }
        if(mb_strlen($_POST['sub_title'])>5){
            response(-1,"话题名称不能大于5个字");
        }
        if(!empty($_POST['sub_desc'])){
            if(mb_strlen($_POST['sub_desc'])>50){
                response(-1,"话题描述不能大于50个字");
            }
            $data['sub_desc']=$_POST['sub_desc'];
        }else{
            $data['sub_desc']="";
        }
        $id=$_REQUEST['id'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        //先去查询下是否已经新建了这个话题
        $is_save1=$this->table->one_save(['sub_title'=>$_POST['sub_title']],$id);
        if($is_save1){
            response(-1,"该话题已经存在");
        }
        $data['sub_title']=$_POST['sub_title'];
        $data['sub_pic']=$_POST['sub_pic'];
        $data['sub_type']=$_POST['sub_type'];
        $res=$this->table->update("type_sub",$data,['id'=>$id]);
        if($res){
            response(0,"修改成功");
        }else{
            response(0,"修改失败");
        }
    }

    /**
     * 排序
     */
    public function paixu(){
        $video_uuid=$_REQUEST['video_uuid'];
        if(empty($video_uuid)){
            response(-1,"参数不能为空");
        }
        $res=$this->table->one("type_sub",["id"=>$video_uuid]);
        if($res['sort']==999999999 || empty($res['sort'])){
            $order=0;
        }else{
            $order=$res['sort'];
        }
        if($res){
            $html="";
            $html.="<br>";
            $html.="<div class=\"layui-form-item\">";
            $html.="<label class=\"layui-form-label\">当前排序</label>";
            $html.="<div class=\"layui-input-block\">";
            $html.="<input type=\"text\" name=\"title\" value=\"{$order}\" placeholder=\"请输入排序\" class=\"layui-input\" disabled='disabled'>";
            $html.="</div>";
            $html.="</div>";
            $html.="<div class=\"layui-form-item\">";
            $html.="<label class=\"layui-form-label\">更改排序</label>";
            $html.="<div class=\"layui-input-block\">";
            $html.="<input type=\"text\" name=\"\" placeholder=\"请输入排序\" class=\"layui-input edit_paixu\">";
            $html.="</div>";
            $html.="</div>";
            response(0,"成功",$html);
        }else{
            response(-1,"失败");
        }
    }

    /**
     * 保存排序
     */
    public function edit_paixu(){
        if(empty($_POST['video_uuid'])){
            response(-1,"参数不能为空");
        }
        if(!is_numeric($_POST['paixu'])){
            response(-1,"排序值不是数字");
        }
        if($_POST['paixu']<0 || $_POST['paixu']>100){
            response(-1,"排序值不合法");
        }
        if($_POST['paixu']==0 || empty($_POST['paixu'])){
            $_POST['paixu']='999999999';
        }
        $id=$_POST['video_uuid'];
        $old_sort=$this->table->one("type_sub",['id'=>$id],"sort");
        $res=$this->table->update_sort($_POST['paixu'],$id,$old_sort['sort']);
//        var_dump($res);exit;
        if($res){
            response(0,"排序成功");
        }else{
            response(-1,"排序失败");
        }
    }

    /**
     * 上线话题
     */
    public function  up_words(){
        $id=$_POST['video_uuid'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        //把原始素材和用户投稿的属于这个话题的变为原来状态，然后再去把这个话题上线线
        $res=$this->table->update($this->type_sub,['online'=>2],['id'=>$id]);
        if($res){
            response(0,"上线成功");
        }else{
            response(-1,"上线失败");
        }
    }

    /**
     * 下线话题
     */
    public function  down_words(){
        $id=$_POST['video_uuid'];
        if(empty($id)){
            response(-1,"缺少参数");
        }
        //把原始素材和用户投稿的属于这个话题的变为下线状态，然后再去把这个话题下线
        $res=$this->table->down_words($id);
        if($res){
            //拼接数据入库
            $data['user_id']=$this->userInfo['role_id'];
            $data['user_name']=$this->userInfo['name'];
            $data['detail_msg']="下线话题，话题id：".$id;
            $data['add_time']=date("Y-m-d H:i:s",time());
            $this->table->add($this->detail_msg,$data);
            response(0,"下线成功");
        }else{
            response(-1,"下线失败");
        }
    }

///////////////////////////////////////////////////////////////////话题结束///////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////所属话题开始////////////////////////////////////////////////////////////////////

    /**
     * 上线所属话题
     */
    public function  up_sort(){
        $video_uuid=$_POST['video_uuid'];
        if(empty($video_uuid)){
            response(-1,"缺少参数");
        }
        $data['examine']=3;
        $res=$this->table->update("user_material",$data,["id"=>$video_uuid]);
        if($res){
            response(0,"上线成功");
        }else{
            response(-1,"上线失败");
        }
    }

    /**
     * 下线所属话题
     */
    public function  down_sort(){
        $video_uuid=$_POST['video_uuid'];
        if(empty($video_uuid)){
            response(-1,"缺少参数");
        }
        $data['examine']=1;
        $data['type_sub_order']=999999999;
        $res=$this->table->update("user_material",$data,["id"=>$video_uuid]);
        if($res){
            //拼接数据入库
            $datas['user_id']=$this->userInfo['role_id'];
            $datas['user_name']=$this->userInfo['name'];
            $datas['detail_msg']="下线所属话题，话题id：".$video_uuid;
            $datas['add_time']=date("Y-m-d H:i:s",time());
            $this->table->add($this->detail_msg,$datas);
            response(0,"下线成功");
        }else{
            response(-1,"下线失败");
        }
    }

    /**
     * 移出所属话题
     */
    public function move_sort(){
        $video_uuid=$_POST['video_uuid'];
        if(empty($video_uuid)){
            response(-1,"缺少参数");
        }
        //先去修改精品视频管理表变为移出话题，再去修改user_material表
        $res=$this->table->update_material($video_uuid);
        if($res){
            //拼接数据入库
            $datas['user_id']=$this->userInfo['role_id'];
            $datas['user_name']=$this->userInfo['name'];
            $datas['detail_msg']="移出所属话题，话题id：".$video_uuid;
            $datas['add_time']=date("Y-m-d H:i:s",time());
            $this->table->add($this->detail_msg,$datas);
            response(0,"移出成功");
        }else{
            response(-1,"移出失败");
        }
    }

    /**
     * 编辑所属话题（不是其他话题）
     */
    public function edit_sort(){
        $video_uuid=$_REQUEST['id'];
        if(empty($video_uuid)){
            response(-1,"缺少参数");
        }
        $res=$this->table->one("user_material",["id"=>$video_uuid]);
//        var_dump($res);exit;
        $data['data']=$res;
        $data['type']=$this->table->types();
        $data['type_sub']=$this->table->type_sub(['sub_type'=>$res['join_type']],4);
        $this->layout("type/word/edit_sort",$data);
    }

    /**
     * 保存编辑所属话题（不是其他话题）
     */
    public function save_edit_sort(){
//        var_dump($_POST);exit;
        if(empty($_POST['fenlei'])){
            response(-1,"分类不能为空");
        }
        if(empty($_POST['huati'])){
            response(-1,"分类不能为空");
        }
//        $length=count($_POST['update_video']);
//        if($length==1){
//            if(empty($_POST['update_video'][0][0]) && empty($_POST['update_video'][0][1])){
//                $data['video_url']=$_POST['video_url1'];
//            }else{
//                $status=$this->check_status($_POST['update_video'],$_POST['duration']);
//                if($status){
//                    //去裁剪视频并合并
//                    var_dump(123);exit;
//                }else{
//                    var_dump(10);exit;
//                    response(-1,"裁剪参数错误，请重新填写");
//                }
//            }
//        }else{
//            $status=$this->check_status($_POST['update_video'],$_POST['duration']);
//            if($status){
//                //去裁剪视频并合并
//                var_dump(124);exit;
//            }else{
////                var_dump(11);exit;
//                response(-1,"裁剪参数错误，请重新填写");
//            }
//        }
//        if($_POST['join_type_sub']!=1){
//            $data['old_join_type_sub']="";
//            $data['examine']=1;
//        }
        $data['join_type']=$_POST['fenlei'];
        $data['join_type_sub']=$_POST['huati'];
        $data['video_desc']=$_POST['video_desc'];
        $data['pic']=$_POST['video_url2'];
        $data['video_url']=$_POST['video_url1'];
//        var_dump($data);exit;
        $res=$this->table->save_edit_sort($data,$_POST['id']);
        if($res){
            response(0,"修改成功");
        }else{
            response(-1,"修改失败");
        }
    }

    /**
     * 编辑所属话题（是其他话题）
     */
    public function edit_sorts(){
        $video_uuid=$_REQUEST['id'];
        if(empty($video_uuid)){
            response(-1,"缺少参数");
        }
        $res=$this->table->one("user_material",["id"=>$video_uuid]);
//        var_dump($res);exit;
        $data['data']=$res;
        $data['type']=$this->table->types();
        $data['type_sub']=$this->table->type_sub(['sub_type'=>$res['join_type']],4);
        $this->layout("type/word/edit_sorts",$data);
    }

    /**
     * 保存编辑所属话题（是其他话题）
     */
    public function save_edit_sorts(){
        if(empty($_POST['fenlei'])){
            response(-1,"分类不能为空");
        }
        if(empty($_POST['huati'])){
            response(-1,"分类不能为空");
        }
        $data['join_type']=$_POST['fenlei'];
        $data['join_type_sub']=$_POST['huati'];
        $data['video_desc']=$_POST['video_desc'];
        $data['pic']=$_POST['video_url2'];
        $data['video_url']=$_POST['video_url1'];
        if($_POST['fenlei']!=1 && $_POST['huati']){
            //根据传的分类和话题不为1的就变味上线状态
            $data['examine']=3;
        }
        $res=$this->table->save_edit_sorts($data,$_POST['id']);
        if($res){
            response(0,"修改成功");
        }else{
            response(-1,"修改失败");
        }
    }

    /**
     * 所属话题排序
     */
    public function sort(){
        $video_uuid=$_REQUEST['video_uuid'];
        if(empty($video_uuid)){
            response(-1,"参数不能为空");
        }
        $res=$this->table->one("user_material",["id"=>$video_uuid]);
        if($res['type_sub_order']==999999999 || empty($res['type_sub_order'])){
            $order=0;
        }else{
            $order=$res['type_sub_order'];
        }
        if($res){
            $html="";
            $html.="<br>";
            $html.="<div class=\"layui-form-item\">";
            $html.="<label class=\"layui-form-label\">当前排序</label>";
            $html.="<div class=\"layui-input-block\">";
            $html.="<input type=\"text\" name=\"title\" value=\"{$order}\" placeholder=\"请输入排序\" class=\"layui-input\" disabled='disabled'>";
            $html.="</div>";
            $html.="</div>";
            $html.="<div class=\"layui-form-item\">";
            $html.="<label class=\"layui-form-label\">更改排序</label>";
            $html.="<div class=\"layui-input-block\">";
            $html.="<input type=\"text\" name=\"\" placeholder=\"请输入排序\" class=\"layui-input edit_paixu\">";
            $html.="</div>";
            $html.="</div>";
            response(0,"成功",$html);
        }else{
            response(-1,"失败");
        }
    }

    /**
     * 保存所属话题排序
     */
    public function save_sort(){
        if(empty($_POST['video_uuid'])){
            response(-1,"参数不能为空");
        }
        if(!is_numeric($_POST['paixu'])){
            response(-1,"排序值不是数字");
        }
        if($_POST['paixu']<0 || $_POST['paixu']>100){
            response(-1,"排序值不合法");
        }
        if($_POST['paixu']==0 || empty($_POST['paixu'])){
            $_POST['paixu']='999999999';
        }
        $old_sort=$this->table->one("user_material",['id'=>$_POST['video_uuid']],"type_sub_order,join_type_sub");
        $res=$this->table->update_sort1($_POST['paixu'],$_POST['video_uuid'],$old_sort['type_sub_order'],$old_sort['join_type_sub']);
        if($res){
            response(0,"排序成功");
        }else{
            response(-1,"排序失败");
        }
    }

///////////////////////////////////////////////////////////////所属话题结束/////////////////////////////////////////////////////////////////////////////////////

    /**
     * 检验裁剪参数是否正确
     */
    public function  check_status($data,$duration){
        if(!is_array($data)){
//            var_dump(1);exit;
            return false;
        }
        $arr=[];
        foreach($data as $k=>$v){
            foreach($v as $kk=>$vv){
                //判断每个数都不是空的
                if(empty($vv)){
//                    var_dump(2);exit;
                    return false;
                }
                //这里是判断填写的每个时间都是大于0的
                if($v[1]<0 || $v[0]<0){
//                    var_dump(3);exit;
                    return false;
                }
                //判断后面一个数比前面一个数大
                if($v[1] < $v[0]){
//                    var_dump(4);exit;
                    return false;
                }
                //判断用户填写的最大值要小于视频时长
                if(ceil($duration) < $vv){
//                    var_dump(5);exit;
                    return false;
                }
            }
            //这里判断视频裁剪所得的总时长是否大于10，小于30
            $arr[]=$v[1]-$v[0];
        }
        //先统计裁剪时间的数组个数
        $length=count($arr);
        //只有一个数组值时直接判断
        if($length==1){
            if($arr[0]<10){
//                var_dump(6);exit;
                return false;
            }else{
//                var_dump(9);exit;
                return $arr[0];
            }
        }else{
            //不止一个时，先遍历数组
            $len=0;
            foreach($arr as $a=>$b){
                $len=$len+$b;
            }
            if($len>30){
//                var_dump(7);exit;
                return false;
            }
            if($len<10){
//                var_dump(8);exit;
                return false;
            }
            return true;
        }
    }

    /**
     * 获取分类
     */
    public function types(){
        $biaoqian=$this->table->types();
        if($biaoqian){
            response(0,"获取成功",$biaoqian);
        }else{
            response(-1,"获取失败",$biaoqian);
        }
    }

    /**
     * 获取话题
     */
    public function type_sub(){
        $where['sub_type']=$_POST['type_id'];
        $biaoqian=$this->table->type_sub($where);
        if($biaoqian){
            response(0,"获取成功",$biaoqian);
        }else{
            response(-1,"当前分类下没有话题",$biaoqian);
        }
    }

    /**
     * 弹出层表格
     */
    public function table(){
        $id=$_REQUEST['type_id'];
        $data['id']=$id;
        $this->load->view("type/word/table",$data);
    }

    /**
     * 加载表格数据
     */
    public function get_table(){
        //分页
        $arr=[];
        $arr['page']=intval($this->get('page'))>0 ? intval($this->get('page')) : 1;
        $arr['limit']=intval($this->get('limit'))>0 ? intval($this->get('limit')) : 10;
        $arr['type_id']=$_GET['type_id'];
        $res=$this->table->get_table($arr);
        if(!empty($res['row'])){
            foreach ($res['row'] as $k=>&$v){
                $input="<input type=\"checkbox\" name=\"like[write]\" value=\"{$v['id']}\" title=\"\">";
                $v['input']=$input;
                $img="<img src=\"{$v['quatily_pic']}\" style='width:80px;height:60px;'>";
                $v['img']=$img;
            }
        }
        //查询话题
        $type_sub=$this->table->type_sub("",2);
        //查询话题
        foreach($res['row'] as $kk=>&$vv){
            foreach($type_sub as $kkk=>$vvv){
                if($vv['quality_join_type_sub']==$vvv['id']){
                    $vv['quality_join_type_sub_name']=$vvv['sub_title'];
                }
            }
        }
        response(0,"成功",$res['row'],$res['total']);
    }

    /**
     * 加入话题
     */
    public function save_edit_table(){
        $arr=$_POST['arr'];
        if(empty($arr)){
            response(-1,"请选择要操作的数据");
        }
        $res=$this->table->update_table($arr);
//        var_dump($res);exit;
        if($res){
            response(0,"加入成功");
        }else{
            response(-1,"加入失败");
        }
    }


}